package com.mf.cloud.resp;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * ClassName: ResultData
 * Package: com.mf.cloud.resp
 *
 * @Author: lmf
 * @Create: 2024/3/6 21:35
 * @Version: 1.0
 * Description:  统一的消息返回
 */
@Data
@Accessors(chain = true)
public class ResultData<T> {
    private String code;
    private String message;
    private T data;
    // 防止请求缓存
    private  long timestamp;
    public ResultData(){
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 成功的返回结果
     * @param data T
     * @return ResultData
     */
    public static <T> ResultData<T> success(T data) {
        final ResultData<T> resultData = new ResultData<>();
        resultData.setCode(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(ReturnCodeEnum.RC200.getMessage());
        resultData.setData(data);
        return resultData;
    }

    /**
     * 失败的返回结果
     * @param code String
     * @return ResultData
     */
    public static <T> ResultData<T> fail(String code) {
        final ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMessage(ReturnCodeEnum.getReturnCodeEnum2(code).getMessage());
        resultData.setData(null);
        return resultData;
    }
    /**
     * 失败的返回结果
     * @param code String
     * @param message String
     */
    public static <T> ResultData<T> fail(String code, String message) {
        final ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMessage(message);
        resultData.setData(null);
        return resultData;
    }
}
